/*
 * =====================================================================================
 *
 *       Filename:  pipe2.c
 *
 *    Description:  demo of how parent/child pipe works
 *
 *        Version:  1.0
 *        Created:  05/04/2012 01:30:34 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhiyi Wu (Z.W), zhiyiw@uoregon.edu
 *        Company:  University of Oregon
 *
 * =====================================================================================
 */

#include <stdio.h>  
#include <unistd.h>  
#include <string.h>  
#include	<wait.h>
#define MAX_LINE 80  
#define PIPE_STDIN 0  
#define PIPE_STDOUT 1  




int main(  )  
{  
//	const char* string={"A simple message."};  
	int ret,thePipe[2]; 
	char buf[MAX_LINE+1];  
	const char* testbuf={"Hello--- from pipe."};  
	if( pipe(thePipe)==0 )  
	{  
		if( fork()==0 )  
		{  
			printf("You have enter the child process\n" );  
			ret=read(thePipe[0],buf,MAX_LINE);//  child read from pipe
			buf[ret]=0;  //assign a null to terminate the string
			printf("Child read info: %s\n",buf);  

		}  
		else  
		{  
			ret=write(thePipe[1],testbuf,strlen(testbuf)); //give the testbuf to pipe  
			ret=wait(NULL);  

		}  
	}  
	close(thePipe[0]);  
	close(thePipe[1]);  

	return 0;  
}
